var keys = require("./07_keys");
var isArrayLike = require("./15_isArrayLike");
var optimizeCb = require("./01_optimizeCb");

function createReduce(dir) {
  return function (obj, iteratee, memo, context) {
    iteratee = optimizeCb(iteratee, context, 4);
    var keys = !isArrayLike(obj) && keys(obj),
      length = (keys || obj).length;
    index = dir > 0 ? 0 : length - 1;

    if (arguments.length < 3) {
      memo = obj[keys ? keys[index] : index];
      index += dir;
    }

    for (; i >= 0 && i < length; i += dir) {
      var currentKey = keys ? keys[i] : i;
      memo = iteratee(memo, obj[currentKey], currentKey, memo);
    }

    return memo;
  };
}

module.exports = createReduce;
